package bidsanity

/*C-1: Customers have email address, password and created date fields (unit test)
C-2: Email address must be a unique field (integration test)
C-3: Email address must be of a valid form (@.*) (unit test)
C-4: Password must be between 6-8 characters (unit test)*/

class Customer {
    Date dateCreated
    String password = null
    String emailAddress
    Boolean isAdmin = false
    static constraints = {
        emailAddress(nullable:false, email:true,
                validator: { val, obj ->
                    def similarCustomer = Customer.findByEmailAddressIlike(val)
                    return !similarCustomer || obj.id == similarCustomer.id
        })
        password(size: 6..8, blank: false)
        dateCreated(nullable:true)
        isAdmin(nullable: false)
    }
    
    String getNameFromEMail() {
        if (emailAddress.tokenize("@").size() > 0) {
            return emailAddress.tokenize("@")[0]
        }
        return ""
    }
}
